package cn.mothin.frame.help;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.stereotype.Component;

@Component("528dd04d2d88c5d61526a555")
public class VersionHelper {
	// 文件路径
	protected static final String CONFIG_FILE = "/ver.properties";
	// 主版本号
	private static final String PROPERTY_NAME_MAJOR = "major";
	// 次版本号
	private static final String PROPERTY_NAME_MINOR = "minor";
	// 修正版本号
	private static final String PROPERTY_NAME_REVISION = "revision";
	// 编译版本号
	private static final String PROPERTY_NAME_BUILD = "build";
	// 发布形式
	private static final String PROPERTY_NAME_MODALITY = "modality";
	// 最后一次发布时间
	private static final String PROPERTY_NAME_TIME = "time";
	// 版本显示模式
	private static final String PROPERTY_NAME_MODE = "mode";

	private Configuration config = null;

	private static String major = null;
	private static String minor = null;
	private static String revision = null;
	private static String build = null;
	private static String modality = null;
	private static String time = null;
	private static int mode = -1;

	public VersionHelper() {
		try {
			config = new PropertiesConfiguration(CONFIG_FILE);
		} catch (ConfigurationException e) {
			config = null;
		}
	}

	public String getVersion() {
		if (config == null) {
			return "ERROR VERSION";
		}

		if (mode == -1) {
			mode = config.getInt(PROPERTY_NAME_MODE, 3);
			if (mode < 0 || mode > 5) {
				mode = 5;
			}
		}
		if (major == null) {
			major = config.getString(PROPERTY_NAME_MAJOR);
		}
		if (minor == null) {
			minor = config.getString(PROPERTY_NAME_MINOR);
		}
		if (revision == null) {
			revision = config.getString(PROPERTY_NAME_REVISION);
		}
		if (build == null) {
			build = config.getString(PROPERTY_NAME_BUILD);
		}
		if (modality == null) {
			modality = config.getString(PROPERTY_NAME_MODALITY);
		}
		if (time == null) {
			time = config.getString(PROPERTY_NAME_TIME);
		}

		if (mode == 0) {
			return major + "." + minor + "." + revision + "." + build
					+ modality + "@" + time;
		} else if (mode == 1) {
			return major + "." + minor + modality + "@" + time;
		} else if (mode == 2) {
			return major + "." + minor + "." + revision + "." + build
					+ modality;
		} else if (mode == 3) {
			return major + "." + minor + modality;
		} else if (mode == 4) {
			return major + "." + minor ;
		} else { //if (mode == 5) 
			return major ;
		}
	}
}
